package com.jiangxiaofei.citycloud.activity.freshfruits;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.activity.MainWebActivity;
import com.jiangxiaofei.citycloud.adapter.FreshHomeAdapter;
import com.jiangxiaofei.citycloud.base.HaveNeedBaseActivity;
import com.jiangxiaofei.citycloud.bean.BannerBean;
import com.jiangxiaofei.citycloud.bean.FreshFruitHomeBean;
import com.jiangxiaofei.citycloud.constants.BannerId;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.httpUtil.APIUrl;
import com.jiangxiaofei.citycloud.mvp.FreshConstant;
import com.jiangxiaofei.citycloud.mvp.FreshPersenter;
import com.jiangxiaofei.citycloud.mvp.MainConstant;
import com.jiangxiaofei.citycloud.mvp.MainPersenter;
import com.jiangxiaofei.citycloud.util.BannerViewHolder;
import com.jiangxiaofei.citycloud.web.SonicJavaScriptInterface;
import com.zhouwei.mzbanner.MZBannerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 作者: JiangXfei
 * 日期: 2017/10/19 0019
 *
 * @title 生鲜果蔬主页
 */

public class FreshFruitsHomeActivity extends HaveNeedBaseActivity
        implements FreshConstant.GetFreshHomeView, MainConstant.GetBannerView {
    @InjectView(R.id.banner)
    MZBannerView banner;
    @InjectView(R.id.linear_have)
    LinearLayout linearHave;
    @InjectView(R.id.linear_need)
    LinearLayout linearNeed;
    @InjectView(R.id.linear_impression)
    LinearLayout linearImpression;
    @InjectView(R.id.linear_cassify)
    LinearLayout linearCassify;
    @InjectView(R.id.recy_fresh_home)
    RecyclerView recyFreshHome;
    private MainConstant.GetBannerPersenter getBannerPersenter;
    private FreshConstant.GetFreshHomePersenter getFreshHomePersenter;
    private List<FreshFruitHomeBean> freshFruitHomeBeanList = new ArrayList<>();
    private FreshHomeAdapter freshHomeAdapter;
    private int freshMenuId = 0;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_fresh_fruits_home_layout);
        ButterKnife.inject(this);
        initSearchTitle();
        mActivity = this;
        mContext = this;
        initAdapter();
        getBannerPersenter = new MainPersenter(mContext, (MainConstant.GetBannerView) mActivity);
        getFreshHomePersenter = new FreshPersenter(mContext, (FreshConstant.GetFreshHomeView) mActivity);
        getBannerPersenter.getBanner();
        getFreshHomePersenter.getFreshHome();
    }

    /**
     * 初始化首页主模块适配器
     */
    private void initAdapter() {
        freshHomeAdapter = new FreshHomeAdapter(R.layout.item_fresh_home_type_layout, freshFruitHomeBeanList);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyFreshHome.setLayoutManager(manager);
        freshHomeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                int typeId = Integer.valueOf(freshFruitHomeBeanList.get(position).getId());
                Intent intent = new Intent(mActivity, FreshFruitGoodListActivity.class);
                intent.putExtra(StringConstants.FRESHGOODCASSIFYID, typeId);
                startActivity(intent);
            }
        });
        recyFreshHome.setAdapter(freshHomeAdapter);

    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    /**
     * 菜单按钮点击事件
     *
     * @param v
     */
    @OnClick({R.id.linear_have, R.id.linear_need, R.id.linear_impression, R.id.linear_cassify})
    public void bOnClick(View v) {

        Intent intent;
        switch (v.getId()) {
            case R.id.linear_need:
                //采购大厅
                freshMenuId = 2;
                intent = new Intent(mContext, FreshFruitsNeedListActivity.class);
                intent.putExtra(StringConstants.FRESHFRUITSTYPEID, freshMenuId);
                startActivity(intent);
                break;
            case R.id.linear_impression:
                //产地印象
                String title = "产地印象";
                String url = APIUrl.API_URL + "/upload/client/chandiyinxiang/index.html";
                intent = new Intent(mContext, MainWebActivity.class);
                intent.putExtra(MainWebActivity.PARAM_URL, url);
                intent.putExtra(MainWebActivity.PARAM_TITLE, title);
                intent.putExtra(SonicJavaScriptInterface.PARAM_CLICK_TIME, System.currentTimeMillis());
                intent.putExtra(MainWebActivity.PARAM_TYPE,"write");
                startActivity(intent);
                break;
            case R.id.linear_cassify:
                //全部分类
                intent = new Intent(mContext, FreshFruitsCassifyActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    public void setData(Object o) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    @Override
    public void getFreshHomeOnSuccess(List<FreshFruitHomeBean> homeBeans) {
        freshFruitHomeBeanList.clear();
        if (homeBeans.size() > 0) {
            freshFruitHomeBeanList.addAll(homeBeans);
        }
        freshHomeAdapter.notifyDataSetChanged();
    }

    @Override
    public int getBannerParam() {
        return BannerId.FRESHHOMEID;
    }

    @Override
    public void bannerSuccess(List<BannerBean> bannerBeans) {
        banner.setPages(bannerBeans, new BannerViewHolder(mContext));
    }
}
